﻿@using OBUCare.Core.Models
@using OBUCare.Web.Models
@using OBUCare.Web.Infrastructure
@using OBUCare.Core.Common;
@{
    string url = Request.Url.ToString();
    var menuName = PageData["moduleID"] == null ? UIUtility.getMenuName(Request.Url.LocalPath) : PageData["moduleID"];

    ViewData["menuName"] = menuName;

}
<h2>@ViewBag.Title</h2>
<link href="../../Content/tableCss.css" rel="stylesheet" type="text/css" />
@Html.Partial("SearchBy")
@using (Html.BeginForm("InsertConfirmed", "Invoice", FormMethod.Post, new { @id = "form_id" }))
{

    @Html.Partial("_VoiceInfo")


    <div>
        @(Html.Telerik().Grid<TransactionLogViewModel>()
        .Name("Grid")
        .DataKeys(keys => keys.Add(c => c.ID))
        .DataBinding(dataBinding => dataBinding.Ajax().
            Select("List", "Invoice", new { IDs = ViewBag.IDs }))
        .Columns(columns =>
        {
            columns.Bound(o => o.ID)
               .ClientTemplate("<input name='checkedRecords' type='checkbox' id='<#= ID #>' onclick='javascript:chooseSameSerial(this);' value='<#= ID #>'  title='选中当前行'/> <#= ++index #>")
               .HeaderTemplate
               (
            @<text>
                <input type="checkbox" title="全选" id="checkAllRecords" />
            </text>
               ).Width("10%");
            columns.Bound(o => o.SERIAL_NO).Width("20%");
            columns.Bound(o => o.TransactionType).Width("10%");
            columns.Bound(o => o.SumPrice).Width("10%").Title("交易金额(元)");
            columns.Bound(o => o.CREATED_DATE).Width("12%");
            columns.Bound(o => o.AgentName).Width("10%").Title("营业厅");
            columns.Bound(o => o.OperatorName).Width("10%").Title("操作员");
            columns.Bound(o => o.OPERATION_KIND).Hidden();
        }).TableHtmlAttributes(new { @style = "width:130%;" })
                                                                      .Footer(false)
                                                                      .Scrollable(scroll => scroll.Height(CommonStyleForUI.Grid_Height))
                                                                      .ClientEvents(events => events.OnDataBinding("onDataBinding"))
        //.Selectable()
        )
    </div>


    <p>
        <input id="btnSubmit" type="button" value="提交" />
        <input id="btnReset" type="button" value="清空" />
    </p>
}
<script type="text/javascript">

    $().ready(function () {
        //为全选框注册事件
        $('#checkAllRecords').click(function () {
            $("#Grid tbody input:checkbox").attr("checked", this.checked);
            var listChoose = $("#Grid tbody input:checkbox[name =checkedRecords][checked]");
            if (listChoose.length <= 10 && listChoose.length>0) {
                for (var i = 0; i < listChoose.length; i++) {
                    var tds = $(listChoose[i]).parent().parent().find("td");
                    var operationkind =parseInt($(tds[7]).html());
                    switch (operationkind) {
                        case  @TransactionType.RepairObu.ToInt():
                        case  @TransactionType.IssueSpareTape.ToInt():
                        case  @TransactionType.IssueBase.ToInt():
                        case @TransactionType.OneRepair.ToInt():
                            $("#INVOICE_TYPE" +(i+1))[0].parentElement.innerHTML="<DIV class='t-dropdown-wrap t-state-default'><SPAN class=t-input>国税</SPAN><SPAN class=t-select><SPAN class='t-icon t-arrow-down'>select</SPAN></SPAN></DIV><INPUT style='DISPLAY: none' id=INVOICE_TYPE"+(i+1)+" value=1 type=text name=INVOICE_TYPE"+(i+1)+" jQuery17107240826925574847='5'>";
                            $("#INVOICENO" + (i+1))[0].innerText="发票号"+(i+1);
                            break;
                        case  @TransactionType.CollectsDeposit.ToInt():
                            $("#INVOICE_TYPE" +(i+1))[0].parentElement.innerHTML="<DIV class='t-dropdown-wrap t-state-default'><SPAN class=t-input>收据</SPAN><SPAN class=t-select><SPAN class='t-icon t-arrow-down'>select</SPAN></SPAN></DIV><INPUT style='DISPLAY: none' id=INVOICE_TYPE"+(i+1)+" value=3 type=text name=INVOICE_TYPE"+(i+1)+" jQuery17107240826925574847='5'>";
                            $("#INVOICENO" + (i+1))[0].innerText="流水单号";
                            $("#INVOICE_NO"+(i+1)).val($(tds[1]).html());
                            break;
                        case  @TransactionType.RepairServiceFee.ToInt():
                            $("#INVOICE_TYPE" +(i+1))[0].parentElement.innerHTML="<DIV class='t-dropdown-wrap t-state-default'><SPAN class=t-input>地税</SPAN><SPAN class=t-select><SPAN class='t-icon t-arrow-down'>select</SPAN></SPAN></DIV><INPUT style='DISPLAY: none' id=INVOICE_TYPE"+(i+1)+" value=2 type=text name=INVOICE_TYPE"+(i+1)+" jQuery17107240826925574847='5'>";
                            $("#INVOICENO" + (i+1))[0].innerText="发票号"+(i+1);
                            break;
                    }
                    var amount=$(tds[3]).html();

                    $("#AMOUNT"+(i+1)).val(amount);
                }
            }
            for(var i=listChoose.length+1;i<=10;i++)
            {
                $("#AMOUNT"+i).val("");
                $("#INVOICE_NO"+i).val("");
            }

        });
        $('#btnReset').click(function () {
            resetAll();
        })
        //获取被选中的ID
        function getCheckedOderIDs() {
            //获取被选中的ID
            var strBuff = new StringBuffer();
            $("#Grid tbody input:checkbox[name =checkedRecords][checked]").each(function () {
                strBuff.append(this.value);
            });
            strIDs = strBuff.arrayToString(",");
            return strIDs;
        }

        $("#btnSubmit").click(function () {
            //debugger;
            strIDs = getCheckedOderIDs();
            if (strIDs.length < 1) {
                alert("请选中要开具发票的信息"); return false;
            }
            else {
                var jdata = { strIDs: strIDs };
                $.post("/Invoice/checkInvoiceExisit", jdata,
                        function (data) {
                            if (data.Success == false) {
                                alert(data.ErrorMsg);
                                return false;
                            }
                            else if (checkAll() == true) {
                                $('#form_id').submit();
                            }
                        });
            }
        })
    });

    function checkAll() {
        //debugger;
        var amount = 0;
        var allV = "";
        for (var i = 1; i <= 10; i++) {
            var v = $("#INVOICE_NO" + i).val();
            allV += v;
            var money = $("#AMOUNT" + i).val();
            if (v != "" && v.length > 0) {
                if (money <= 0 ) {
                    alert("金额必须大于0"); return false;
                }else if(/^(\+|-)?\d+$/.test( money ) ==false)
                {
                    alert("金额输入不正确");return false;
                }
                else {
                    amount += parseInt(money);
                }
            }
        }
        if ($.trim(allV).length <= 0) {
            alert("请输入发票号"); return false;
        }

        // var list = $("#Grid tbody input:checkbox[checked=checked]");
        var list = $("#Grid tbody input:checkbox[name =checkedRecords][checked]");

        var sumAmount = 0;
        for (var i = 0; i < list.length; i++) {
            var tds = $(list[i]).parent().parent().find("td");
            sumAmount += parseInt($(tds[3]).html());
        }
        if (amount > sumAmount) {
            alert("错误：输入的金额大于流水单的金额，请重新输入！");
            return false;
        }
        return true;

    }

    var index = 0;

    function onDataBinding(e) {
        var grid = $('#Grid').data('tGrid')
        index = (grid.currentPage - 1) * grid.pageSize;
        e.data = {
            // the key ("category") specifies the variable name of the action method which will contain the specified value
            searchSql: $('#searchSql').val(),
            AgentNo: $('#AgentNo').val()
        };
    }
    function chooseSameSerial(modelValue) {
        if (modelValue.value == null) {
            alert("流水单号为空，不允许操作。");
            return false;
        }
        var list = $("#Grid tbody input:checkbox[name =checkedRecords]");

        for (var i = 0; i < list.length; i++) {

            var tds = $(list[i]).parent().parent().find("td");

            var serialID = $.trim(tds[0].children[0].id) == null ? "" : $.trim(tds[0].children[0].id);

            if (modelValue.value == serialID) {
                if (modelValue.checked == true)
                    tds[0].children[0].checked = true;
                else
                    tds[0].children[0].checked = false;
            }
        }
        //debugger;
        var listChoose = $("#Grid tbody input:checkbox[name =checkedRecords][checked]");
        if (listChoose.length <= 10 && listChoose.length>0) {
            for (var i = 0; i < listChoose.length; i++) {
                var tds = $(listChoose[i]).parent().parent().find("td");
                var operationkind =parseInt($(tds[7]).html());
                switch (operationkind) {
                    case  @TransactionType.RepairObu.ToInt():
                    case  @TransactionType.IssueSpareTape.ToInt():
                    case  @TransactionType.IssueBase.ToInt():
                    case  @TransactionType.OneRepair.ToInt():
                        $("#INVOICE_TYPE" +(i+1))[0].parentElement.innerHTML="<DIV class='t-dropdown-wrap t-state-default'><SPAN class=t-input>国税</SPAN><SPAN class=t-select><SPAN class='t-icon t-arrow-down'>select</SPAN></SPAN></DIV><INPUT style='DISPLAY: none' id=INVOICE_TYPE"+(i+1)+" value=1 type=text name=INVOICE_TYPE"+(i+1)+" jQuery17107240826925574847='5'>";
                        $("#INVOICENO" + (i+1))[0].innerText="发票号"+(i+1);
                        break;
                    case  @TransactionType.CollectsDeposit.ToInt():
                        $("#INVOICE_TYPE" +(i+1))[0].parentElement.innerHTML="<DIV class='t-dropdown-wrap t-state-default'><SPAN class=t-input>收据</SPAN><SPAN class=t-select><SPAN class='t-icon t-arrow-down'>select</SPAN></SPAN></DIV><INPUT style='DISPLAY: none' id=INVOICE_TYPE"+(i+1)+" value=3 type=text name=INVOICE_TYPE"+(i+1)+" jQuery17107240826925574847='5'>";
                        $("#INVOICENO" + (i+1))[0].innerText="流水单号";
                        $("#INVOICE_NO"+(i+1)).val($(tds[1]).html());
                        break;
                    case  @TransactionType.RepairServiceFee.ToInt():
                        $("#INVOICE_TYPE" +(i+1))[0].parentElement.innerHTML="<DIV class='t-dropdown-wrap t-state-default'><SPAN class=t-input>地税</SPAN><SPAN class=t-select><SPAN class='t-icon t-arrow-down'>select</SPAN></SPAN></DIV><INPUT style='DISPLAY: none' id=INVOICE_TYPE"+(i+1)+" value=2 type=text name=INVOICE_TYPE"+(i+1)+" jQuery17107240826925574847='5'>";
                        $("#INVOICENO" + (i+1))[0].innerText="发票号"+(i+1);
                        break;
                }
                var amount=$(tds[3]).html();

                $("#AMOUNT"+(i+1)).val(amount);
            }
        }
        for(var i=listChoose.length+1;i<=10;i++)
        {
            $("#AMOUNT"+i).val("");
            $("#INVOICE_NO"+i).val("");
        }

    }
    function resetAll() {
        document.getElementById("form_id").reset();
    }
</script>
